package dao.impl;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import dao.UserDao;
import dbpool.DbPool;
import entity.User;

public class UserDaoImpl implements UserDao {

	QueryRunner runner = new QueryRunner();
	DbPool dbpool = DbPool.getPool();
	
	/*
	 * 
	 * */
	@Override
	public User loginByNameAndPwd(String qqnum, String pwd) throws SQLException {
		String sql = "select * from user where user_qqnum=? and user_pass=?";
		Connection conn = dbpool.getConn();
		User user = (User) runner.query(conn, sql, new BeanHandler<User>(User.class),qqnum,pwd);
		dbpool.back(conn);
		return user;
	}

	@Override
	public int Register(String qqnum, String pwd,String nicheng) throws SQLException {
		String sql = "insert into user values(null,?,?,?)";
		Connection conn = dbpool.getConn();
		int n = runner.update(conn, sql, qqnum,pwd,nicheng);
		dbpool.back(conn);
		return n;
	}

	@Override
	public User queryUserById(int user_id) throws SQLException {
		String sql = "select * from user where user_id=?";
		Connection conn = dbpool.getConn();
		User user = (User) runner.query(conn, sql, new BeanHandler<User>(User.class),user_id);
		dbpool.back(conn);
		return user;
	}

}
